package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.enums.ordercenter.SalesOrgTypeEnum;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.SalesOrgAbilityAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.request.SalesBizOrgByBuQueryRequest;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.dataobject.response.SalesBizOrgQueryDTO;
import com.alibaba.citrus.cr.mn.order.center.facade.function.dataobject.AtomOrderDO;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangxiaorui
 * @ClassName AbstractOrderCreateParamsCheck
 * @description: TODO
 * @date 2023/05/11 16:48
 * @version: 1.0
 */
public abstract class AbstractOrderParamsConvertService<Request, ModelDO> {

    public abstract List<ModelDO> convert(List<Request> requestList);

    @Resource
    private SalesOrgAbilityAdapter salesOrgAbilityAdapter;

    public Map<String, SalesBizOrgQueryDTO> findSalesOrgByAtomOrderDO(List<AtomOrderDO> orderDOS) {
        List<SalesBizOrgByBuQueryRequest> salesBizOrgByBuQueryRequests = Lists.newArrayList();

        // 业务单元Code、组织Code、部门Code、渠道Code
        Map<String, List<AtomOrderDO>> productGroupOfAtomeOrderMap = orderDOS.stream().collect(Collectors.groupingBy(AtomOrderDO::getProductGroupCode));
        productGroupOfAtomeOrderMap.forEach((productGroupCode, AtomOrderDOList) -> {
            List<String> organizationCodes = Lists.newArrayList();
            for (AtomOrderDO orderDO : orderDOS) {
                CollectionUtils.addIgnoreNull(organizationCodes, orderDO.getOrder().getBisUnitCode());
                CollectionUtils.addIgnoreNull(organizationCodes, orderDO.getOrder().getOrganizationCode());
                CollectionUtils.addIgnoreNull(organizationCodes, orderDO.getOrder().getDepartmentCode());
                CollectionUtils.addIgnoreNull(organizationCodes, orderDO.getOrder().getSaleChannelCode());
            }
            for (String organizationCode : organizationCodes) {
                SalesBizOrgByBuQueryRequest buQueryRequest = new SalesBizOrgByBuQueryRequest();
                buQueryRequest.setOrgCode(organizationCode);
                buQueryRequest.setBusinessType(productGroupCode);
                salesBizOrgByBuQueryRequests.add(buQueryRequest);
            }
        });

        List<SalesBizOrgQueryDTO> salesBizOrgList = salesOrgAbilityAdapter.querySalesOrgByCodeAndBU(salesBizOrgByBuQueryRequests);

        if(CollectionUtils.isEmpty(salesBizOrgList)) {
            return Maps.newHashMap();
        }

        Map<String, SalesBizOrgQueryDTO> resultMap = Maps.newHashMap();
        for (SalesBizOrgQueryDTO p : salesBizOrgList) {
            if(String.valueOf(SalesOrgTypeEnum.SALES_ORG.getCode()).equals(p.getOrgType())) {
                // 针对销售组织特殊处理
                resultMap.put(p.getOrgType() + "_" + p.getOrgCode(), p);
            } else {
                resultMap.put(p.getOrgCode(), p);
            }
        }

        return resultMap;
    }

}
